
package character_9;

import com.sun.glass.ui.SystemClipboard;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.TreeSet;
import static java.util.Comparator.*;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.Comparator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyDefine<T> {
    private T t;
    
    public MyDefine(T t){
        this.t = t;
    }
    
    public <T> T getT(T t){
        //Class.forName("");
        return t;
    }
    
    public T getT(){
        return t;
    }
    
    public static void main(String[] args) {
        MyDefine<Test> md = new MyDefine<Test>(new Test("haha", 1));
        if(md.getT(new Test("hehe", 1)) instanceof Test){
            System.out.println("right");
        }
        
        Set set = new TreeSet();
        set.add(new Test("1", 3));
        set.add(new Test("2", 0));
        set.add(new Test("3", 2));
        set.add(new Test("3", 2));
        System.out.println("size" + set.size());
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next().toString());
        }
        System.out.println("==============");
        Queue queue = new PriorityQueue();
        queue.add(new Test("1", 3));
        queue.add(new Test("2", 2));
        queue.add(new Test("3", 5));
        it = queue.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println("==============");
        queue.forEach(ele -> System.out.println(ele));
        System.out.println("==============");
        queue.forEach(new Consumer<Test>() {
            @Override
            public void accept(Test t) {
                System.out.println(t);
            }
        });
        
        List words = Arrays.asList("1", "A", null, "W", "D" ,null);
        words.sort(nullsFirst(reverseOrder()));
        System.out.println(words);
         System.out.println("======Map========");
        Map<String, Test> map = new HashMap<String, Test>();
        map.put("1", new Test("1", 1));
        map.put("2", new Test("2", 2));
        map.put("2", new Test("3", 3));
        System.out.println("size" + map.size() + "\n" + map);
        
        Map<Test, String> map2 = new TreeMap<Test, String>(new Comparator<Test>(){
            public int compare(Test t1, Test t2){
                return t1.i - t2.i;
            }
        });
        map2.put(new Test("1", 4), "de");
        map2.put(new Test("2", 2), "ha");
        map2.put(new Test("3", 3), "he");
        System.out.println("map2:" + map2);
        Map<String, Test> msg = new TreeMap<String, Test>((s1, s2) -> s1.compareTo(s2));
        
        Properties p = new Properties();
        try {
            p.loadFromXML(new FileInputStream("C:\\Users\\Administrator\\Desktop\\JavaLearningNotes\\src\\character_9\\src\\property\\newXMLDocument.xml"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MyDefine.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MyDefine.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("name:" + p.getProperty("name"));
        
        map.keySet().forEach(new Consumer<String>() {
            @Override
            public void accept(String t) {
                System.out.println(t);
            }
        });
        map.values().forEach(new Consumer<Test>() {
            @Override
            public void accept(Test t) {
                System.out.println("Test:" + t);
            }
        });
        forreach(map.entrySet());
    }
    static void forreach(Iterable<Map.Entry<String, Test>> iterable){
        for (Map.Entry<String, Test> entry : iterable) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}
class Test implements Comparable<Test>{
    private String s;
    public int i;
    public Test(String s, int i){
        this.s = s;
        this.i = i;
    }
    public String getString(){
        return this.s;
    }

    @Override
    public int compareTo(Test o) {
        return -(this.i - o.i);
    }

    @Override
    public String toString() {
        return "Test{" + "s=" + s + ", i=" + i + '}';
    }


    
    
}